
var load_char=function (bingDiv,dayDiv,weekDiv) {
    var dormId = localStorage['dormId'];
    $.ajax({
        url: "dorm/detail",
        type: "post",
        data: {
            "token": localStorage['token'],
            "id": dormId
        },
        success: function (data) {
            if (data.code === 200) {
                console.log(data);
                // $("#dormNum").val(data.data.tungNum + "-" + data.data.roomNum);
                setBingChar(bingDiv, data.data.total, data.data.consume);
                // message("请求数据成功", "", "success", 1500);
                // $("#updateTime").val( formatTime (data.data.updateTime));
                // load_close(loadDiv);
            } else {
                errorMessage(data.msg);
            }
        }
    });
    $.ajax({
        url:"dorm/day",
        type: "post",
        data: {
            "token": localStorage['token'],
            "dormId": dormId
        },
        success:function (data) {
            if (data.code === 200) {
                console.log(data);
                setDayXianChar(dayDiv,data.data);
            } else {
                errorMessage(data.msg);
            }
        }
    })

    $.ajax({
        url:"dorm/week",
        type: "post",
        data: {
            "token": localStorage['token'],
            "dormId": dormId
        },
        success:function (data) {
            if (data.code === 200) {
                console.log(data);
                setWeekXianChar(weekDiv,data.data);
            } else {
                errorMessage(data.msg);
            }
        }
    })
}

var setBingChar = function (charDiv, total, consume) {
    charDiv.title = '环形图';
    option = {
        title: {
            text: '宿舍电量情况',
            x: 'center',
            top: 20,
            textStyle: {
                color: '#6FADDE'
            }
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            right: 25,
            top: 40,
            bottom: 20,

        },
        series: [
            {
                name: '详情',
                type: 'pie',
                radius: '55%',
                center: ['50%', '55%'],
                data: [
                    {value: total - consume, name: '剩余电量'},
                    {value: consume, name: '已用电量'}
                ],//数据，数据中其他属性，查阅文档
                itemStyle: {
                    normal: {
                        color: (params) => {
                            let colorList = ['#6FADDE', '#F37570'];
                            return colorList[params.dataIndex]
                        }
                    },
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    charDiv.setOption(option);
    // console.log("success");
};

var setDayXianChar = function (charDiv,comsume) {
    charDiv.title = '折线图';
    option = {
        title: {
            text: '本日电量消耗情况',
            x: 'center',
            top: 20,
            textStyle: {
                color: '#6FADDE'
            }
        },
        xAxis: {
            type: 'category',
            data: ['0:00', '04:00', '08:00', '12:00', '16:00', '20:00', '24:00']
        },
        tooltip: {
            trigger: 'axis'
        },
        yAxis: {
            type: 'value'
        },

        series: [{
            data: comsume,
            y: "55%",
            type: 'line',
            itemStyle: {
                normal: {
                    color: "#6FADDE",
                }
            }
        }]
    };
    charDiv.setOption(option);
};

var setWeekXianChar = function (charDiv,data) {
    charDiv.title = '折线图';
    option = {
        title: {
            text: '本周电量消耗情况',
            x: 'center',
            top: 20,
            textStyle: {
                color: '#6FADDE'
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },

        tooltip: {
            trigger: 'axis'
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            data: data,
            type: 'line',
            y: "55%",
            areaStyle: {},
            itemStyle: {
                normal: {
                    color: "#6FADDE",
                }
            }
        }]
    };
    charDiv.setOption(option);
};

var add_repair_data = function (pageNumber) {
    var loadDiv = load(".list-content",99999);
    $(".list-content").children("table").children("tbody").html("");
    $(".chooseNum").children("select").html("");
    $.ajax({
        url: "repairTask/list",
        type: "post",
        data: {
            "token": localStorage['token'],
            "pageSize": 3,
            "pageNumber": pageNumber
        },
        success: function (data) {
            console.log(data);
            if (data.code == 200) {
                console.log(1);
                var length = data.data.content.length;
                listSetting(data.data.totalPages, data.data.pageNumber);
                // localStorage['pageSize'] = data.data.pageSize + "";
                // localStorage['pageNumber'] = data.data.pageNumber + "";
                for (var j = 0; j < data.data.totalPages; j++) {
                    var option = addRealOption(j + 1);
                    if ((j + 1) == data.data.pageNumber) {
                        option.attr("selected", "selected");
                    }
                    option.appendTo($(".chooseNum").children("select"));
                }
                ;
                for (var i = 0; i < length; i++) {
                    var task = data.data.content[i];
                    var tr = $('<tr></tr>');
                    var state = stateChangeText(task.state);
                    addTd(task.device).appendTo(tr);
                    addTd(task.content).appendTo(tr);
                    // addTd(formatTime(task.updateTime)).appendTo(tr);
                    addTd(state).appendTo(tr);
                    // $(' <td>\n' + ' <button  onclick="repair_detail($(this))" >查看</button>\n' + ' </td>').appendTo(tr);
                    // addTdHidId(task.id).appendTo(tr);
                    // addTdHidId(task.state).appendTo(tr);
                    tr.appendTo($(".list-content").children("table").children("tbody"))
                }
                $(".thisNum").children("span").text(data.data.pageNumber);
                // console.log(new Date().getSeconds() );
                if ($(".list-content").children("table").children("tbody").text() === "") {
                    var bk = getNothingBackground();
                    bk.appendTo($(".list-content").children("table").children("tbody"));
                }
            }
            load_close(loadDiv);
        }
    })
};

$("#next").click(function () {
    if (!$(this).hasClass("notAllow")) {
        var pageNumber = parseInt($(".thisNum").children("span").text()) + 1;
        // load($(".list"), 1000);
        add_repair_data(pageNumber);
    }

});

$("#last").click(function () {
    if (!$(this).hasClass("notAllow")) {
        var pageNumber = parseInt($(".thisNum").children("span").text()) - 1;
        add_repair_data(pageNumber);
    }

});


$(".chooseNum").children("select").change(function () {

    add_repair_data($(this).val());
});

var stateChangeText = function (code) {
    var msg = "";
    if (code == 0) {
        msg = "审核中";
    } else if (code == 1) {
        msg = "分配任务中";
    } else if (code == 2) {
        msg = "处理中";
    } else if (code == 3) {
        msg = "已处理";
    } else if (code == 4) {
        msg = "未通过审核";
    }
    return msg;
};